import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		loginProvider: "",
		openid: null,
		forcedLogin: true,
        userName: uni.getStorageSync('userName'),
		mainMoney: uni.getStorageSync('mainMoney'),
		token: uni.getStorageSync('token'),
		member:uni.getStorageSync('member'),
		hasLogin: uni.getStorageSync('hasLogin'),
        inviteCode:uni.getStorageSync('inviteCode'), 
        apiUrl : 'http://m.chuanqi668.xyz/', //移动端接口
		payApiUrl : 'http://pay.xiikk.top/pay/', //支付接口
		payAppkey : "24eaa27719b8bf698b1c8a89c52815c3",//支付秘钥
		gameApiUrl : 'http://game.xiikk.top/game/6', //游戏接口
		gameApiId : '6',
		gameApiSecret : 'b7117837dcdd91da145a8993c7f2d37e',
		wxShareUrl : 'http://mm.tylbd.top/wechat/6?invent=', //移动端接口

		
	},
	mutations: {
		login(state, userName, mainMoney, token) {
			
			state.hasLogin = true;
			state.userName = userName;
			uni.setStorage({
				key: 'userName',
				data: userName,
				success: function () {
					console.log('success');
				}
			});
			uni.setStorage({
				key: 'hasLogin',
				data: true,
				success: function () {
					console.log('success');
				}
			});
			
			
			
		},
		storeMoney(state, mainMoney) {
			console.log('money' + mainMoney);
			state.mainMoney = mainMoney;
			uni.setStorage({
				key: 'mainMoney',
				data: mainMoney,
				success: function () {
					console.log('success');
				}
			});
			
		},
		storeToken(state, token) {
			console.log('story' + token);
			state.token = token;
			uni.setStorage({
				key: 'token',
				data: token,
				success: function () {
					console.log('success');
				}
			});
			
		},
		storeInviteCode(state, inviteCode) {
			console.log('inviteCode' + inviteCode);
			state.inviteCode = inviteCode;
			uni.setStorage({
				key: 'inviteCode',
				data: inviteCode,
				success: function () {
					console.log('success');
				}
			});
			
		},
		storeMember(state, member) {
			console.log('member' + member);
			state.member = member;
			uni.setStorage({
				key: 'member',
				data: member,
				success: function () {
					console.log('success');
				}
			});
			
		},
		logout(state) {
			state.hasLogin = false
			state.openid = null,
			//state.userName = "";
			uni.clearStorage();
		},
		setOpenid(state, openid) {
			state.openid = openid
		},
		back(state) {
			uni.navigateBack({
				delta: 1
			});
		},
		decodeUnicode(str) {
			console.log('23432' + str);
			str = str.toString().substring(2);
			console.log(str);
			str = str.toString().replace(/\\/g, "%");
			return unescape(str);
		}

	},
	actions: {
		// lazy loading openid
		getUserOpenId: async function ({
			commit,
			state
		}) {
			return await new Promise((resolve, reject) => {
				if (state.openid) {
					resolve(state.openid)
				} else {
					uni.login({
						success: (data) => {
							commit('login')
							setTimeout(function () { //模拟异步请求服务器获取 openid
								const openid = '123456789'
								console.log('uni.request mock openid[' + openid + ']');
								commit('setOpenid', openid)
								resolve(openid)
							}, 1000)
						},
						fail: (err) => {
							console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err)
							reject(err)
						}
					})
				}
			})
		}
	}
})

export default store
